import chardet
import configparser,os,json,sys
import platform


config = configparser.ConfigParser()

if platform.machine().startswith('arm') and platform.system() == 'Linux':
    config.read('/home/pi/MindSCADA/runtime.ini',encoding='utf-8')
else:
    config.read('C:/mindteco/MindSCADA/runtime.ini',encoding='utf-8')

# server 路径
serverPath = config.get('General', 'CurrentProjectPath')

#解决中文乱码
# serverPath = os.path.abspath(os.path.normpath(serverPath))
# print('加载serverFiles.py')


# 检测文件编码方式获取配置内容
def getJSON(*path):
    fullPath = os.path.join(*path)
    with open(fullPath, 'rb') as f:
        result = chardet.detect(f.read())
        
    with open(fullPath,encoding=result['encoding']) as f:
        # datasource 文件
        try:
            return json.load(f)
        except json.JSONDecodeError as e:
            print(f"JSON解析错误: {e}")
            print(f"被解析的json路径: {fullPath}")
            sys.exit(1)#退出程序


# Variable.config 文件
dataSource = getJSON(serverPath,'DataArchives','dataSource.json')

# Variable.config 文件
variable = getJSON(serverPath,'RTDB','Variable.config')
# print(variable)
prj_dict = getJSON(serverPath,'dict','project.dict')

Account = getJSON(serverPath,'SecurityPolicy','Account.dat')
